﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using TmU220PrintHelperV2d0.Entity;
using TmU220PrintHelperV2d0.Service;
using ZhangLogSysV1d0;

namespace TmU220PrintHelperV2d0.Singleton
{
    /// <summary>
    /// PHP接口单例
    /// </summary>
    public class PhpApiSetSingletonV1d0
    {
        private static PhpApiSetSingletonV1d0 _instance = null;
        private ThinkPhpApiGroupSetV1d0 _apiGroupSet = null;
        private static readonly object syslock = new object();
        /// <summary>
        /// 单例模式
        /// </summary>
        public static PhpApiSetSingletonV1d0 Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (syslock)
                    {
                        if (_instance == null)
                        {
                            _instance = new PhpApiSetSingletonV1d0();
                        }
                    }
                }
                return _instance;
            }
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        private PhpApiSetSingletonV1d0()
        {
            _apiGroupSet = new ThinkPhpApiGroupSetV1d0();
            LoadData();
        }
        /// <summary>
        /// 装载数据
        /// </summary>
        private void LoadData()
        {
            string currDir = System.IO.Directory.GetCurrentDirectory();
            string filename = currDir + @"\parameter\phpApiSet.xml";
            _apiGroupSet = HotchPotchV2d0.ReadFromXmlFile<ThinkPhpApiGroupSetV1d0>(filename);
        }
        private ThinkPhpApiGroupV1d0 GetApiGroup(string groupName)
        {
            ThinkPhpApiGroupV1d0 apiGroup = null;
            apiGroup = _apiGroupSet.ApiGroupList.FirstOrDefault(x => x.group_name == groupName);
            return apiGroup;
        }
        public ThinkPhpApiV1d0 GetApi(string groupName, string apiName)
        {
            ThinkPhpApiV1d0 api = null;
            var apiGroup = GetApiGroup(groupName);
            if (apiGroup != null)
            {
                string mainUrl = apiGroup.main_url;
                var apiList = apiGroup.ApiList;
                api = apiList.FirstOrDefault(x => x.name == apiName);
                api.main_url = mainUrl;
            }
            return api;
        }
        public string GetProjectMainUrl(string groupName= "TaskApi")
        {
            var apiGroup = GetApiGroup(groupName);
            return apiGroup.main_url;
        }
    }

}
